package com.hy.frame.mvvm.net

import android.net.ParseException
import com.google.gson.JsonParseException
import com.google.gson.stream.MalformedJsonException
import org.json.JSONException
import retrofit2.HttpException
import java.net.ConnectException


object ExceptionHandle {

    fun handleException(e: Throwable?): AppException {
        return when (e) {
//                is CancellationException -> {//取消请求
//                    ex = AppException(Error.CANCEL, e)
//                    return ex
//                }
            is HttpException -> {
                AppException(Error.ERROR_NETWORK, e)
            }
            is JsonParseException, is JSONException, is ParseException, is MalformedJsonException -> {
                AppException(Error.ERROR_PARSE, e)
            }
            is ConnectException -> {
                AppException(Error.ERROR_NETWORK, e)
            }
            is javax.net.ssl.SSLException -> {
                AppException(Error.ERROR_SSL, e)
            }
            is java.net.SocketTimeoutException -> {
                AppException(Error.ERROR_TIMEOUT, e)
            }
            is java.net.UnknownHostException -> {
                AppException(Error.ERROR_TIMEOUT, e)
            }
            is AppException -> {
                e
            }
            else -> {
                AppException(Error.UNKNOWN, e)
            }
        }
    }
}